// 模拟包裹数据（实际需从后端接口获取）
const packages = [
  {
    trackingNo: "FCA000123456789",
    fromCampus: "Campus A",
    toCampus: "Campus B",
    status: "Delivering",
  },
  {
    trackingNo: "FCA000987654321",
    fromCampus: "Campus C",
    toCampus: "Campus D",
    status: "Delivered",
  },
  {
    trackingNo: "FCA000112233445",
    fromCampus: "Campus E",
    toCampus: "Campus F",
    status: "Picked up",
  },
  {
    trackingNo: "FCA000556677889",
    fromCampus: "Campus G",
    toCampus: "Campus H",
    status: "In transit",
  },
  {
    trackingNo: "FCA000121314151",
    fromCampus: "Campus I",
    toCampus: "Campus J",
    status: "Delivering",
  },
  {
    trackingNo: "FCA000617181920",
    fromCampus: "Campus K",
    toCampus: "Campus L",
    status: "Delivered",
  },
  {
    trackingNo: "FCA000212223242",
    fromCampus: "Campus M",
    toCampus: "Campus N",
    status: "Picked up",
  },
  {
    trackingNo: "FCA000252627282",
    fromCampus: "Campus O",
    toCampus: "Campus P",
    status: "In transit",
  },
  {
    trackingNo: "FCA000313233343",
    fromCampus: "Campus Q",
    toCampus: "Campus R",
    status: "Delivering",
  },
  {
    trackingNo: "FCA000353637383",
    fromCampus: "Campus S",
    toCampus: "Campus T",
    status: "Delivered",
  },
  {
    trackingNo: "FCA000414243444",
    fromCampus: "Campus U",
    toCampus: "Campus V",
    status: "Picked up",
  },
  {
    trackingNo: "FCA000454647484",
    fromCampus: "Campus W",
    toCampus: "Campus X",
    status: "In transit",
  },
  {
    trackingNo: "FCA000515253545",
    fromCampus: "Campus Y",
    toCampus: "Campus Z",
    status: "Delivering",
  },
  {
    trackingNo: "FCA000555657585",
    fromCampus: "Campus AA",
    toCampus: "Campus BB",
    status: "Delivered",
  },
  {
    trackingNo: "FCA000616263646",
    fromCampus: "Campus CC",
    toCampus: "Campus DD",
    status: "Picked up",
  },
  {
    trackingNo: "FCA000656667686",
    fromCampus: "Campus EE",
    toCampus: "Campus FF",
    status: "In transit",
  },
  {
    trackingNo: "FCA000717273747",
    fromCampus: "Campus GG",
    toCampus: "Campus HH",
    status: "Delivering",
  },
  {
    trackingNo: "FCA000757677787",
    fromCampus: "Campus II",
    toCampus: "Campus JJ",
    status: "Delivered",
  },
  {
    trackingNo: "FCA000818283848",
    fromCampus: "Campus KK",
    toCampus: "Campus LL",
    status: "Picked up",
  },
  {
    trackingNo: "FCA000858687888",
    fromCampus: "Campus MM",
    toCampus: "Campus NN",
    status: "In transit",
  },
];

// 状态管理：当前页、每页条数、过滤/搜索后的结果
let currentPage = 1;
const itemsPerPage = 10;
let filteredPackages = [...packages];

// DOM 元素
const packageTableBody = document.getElementById("packageTableBody");
const prevPageBtn = document.getElementById("prevPage");
const nextPageBtn = document.getElementById("nextPage");
const currentRangeEl = document.getElementById("currentRange");
const totalItemsEl = document.getElementById("totalItems");
const searchBtn = document.getElementById("searchBtn");
const trackingNoSearch = document.getElementById("trackingNoSearch");
const fromCampusSearch = document.getElementById("fromCampusSearch");
const toCampusSearch = document.getElementById("toCampusSearch");

// 过滤显示的复选框
const showTrackingNo = document.getElementById("showTrackingNo");
const showFromCampus = document.getElementById("showFromCampus");
const showToCampus = document.getElementById("showToCampus");
const showStatus = document.getElementById("showStatus");
const showDetailBtn = document.getElementById("showDetailBtn");

// 表头（用于控制列显示）
const trackingNoHeader = document.getElementById("trackingNoHeader");
const fromCampusHeader = document.getElementById("fromCampusHeader");
const toCampusHeader = document.getElementById("toCampusHeader");
const statusHeader = document.getElementById("statusHeader");
const detailLinkHeader = document.getElementById("detailLinkHeader");

// 渲染包裹列表
function renderPackages() {
  packageTableBody.innerHTML = "";

  // 计算当前页数据
  const startIdx = (currentPage - 1) * itemsPerPage;
  const endIdx = Math.min(startIdx + itemsPerPage, filteredPackages.length);
  const currentPagePackages = filteredPackages.slice(startIdx, endIdx);

  // 渲染每行数据
  currentPagePackages.forEach((pkg) => {
    const row = document.createElement("tr");
    row.className = "hover:bg-gray-50";

    // 运单号列
    const trackingNoCell = document.createElement("td");
    trackingNoCell.className = "px-6 py-4 whitespace-nowrap";
    trackingNoCell.textContent = pkg.trackingNo;
    row.appendChild(trackingNoCell);

    // 来源站列
    const fromCampusCell = document.createElement("td");
    fromCampusCell.className = "px-6 py-4 whitespace-nowrap";
    fromCampusCell.textContent = pkg.fromCampus;
    row.appendChild(fromCampusCell);

    // 目标站点列
    const toCampusCell = document.createElement("td");
    toCampusCell.className = "px-6 py-4 whitespace-nowrap";
    toCampusCell.textContent = ""